import 'package:flutter/material.dart';
import 'package:flutterdemo/day10/model/user_model.dart';
import 'package:flutterdemo/day10/viewmodel/counter_view_model.dart';
import 'package:flutterdemo/day10/viewmodel/initalize_providers.dart';
import 'package:flutterdemo/day10/viewmodel/user_view_model.dart';
import 'package:provider/provider.dart';
 /*
  * 1、创建自己需要共享的数据
  * 2、在应用层序的顶层 ChangeNotifierProvider
  * 3、在其他的位置使用共享的数据
  *  - Provider.of
  *  - Consumer
  */

 /*
  * void main() {
      runApp(
        ChangeNotifierProvider(
           child: ChangeNotifierProvider(
             child: MyApp(),
             create:  (ctx) => JKUserViewModel(UserModel('wc'), 28, 'http://ssss'),
           ),
            create: (ctx) => JKCounterViewModel(),
        )
      );
    }
  */
//void main() {
//  runApp(
//      ChangeNotifierProvider(
//        child: ChangeNotifierProvider(
//          child: MyApp(),
//          create:  (ctx) => JKUserViewModel(UserModel('wc', 28, 'http://ssss')),
//        ),
//        create: (ctx) => JKCounterViewModel(),
//      )
//  );
//}

void main() {
  runApp(
      MultiProvider(
        child: MyApp(),
        providers: providers
      )
  );
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // 启动要显示的界面
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("图标组件示例"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            JKShowData01(),
            JKShowData02(),
            JKShowData03()
          ],
        ),
      ),
      floatingActionButton: Selector<JKCounterViewModel, JKCounterViewModel>(
        selector: (ctx, counterVM) => counterVM,
        /* 要不要重新构建，true 那么loatingActionButton 的 builder 方法会被执行，反之false不会被执行*/
        shouldRebuild: (previous, next) => false,
        builder: (ctx, counterVM, child) {
          print('floatingActionButton 的 builder 方法被执行');
          return FloatingActionButton(
            child: child,
            onPressed: () {
              counterVM.counter += 1;
            },
          );
        },
        child: Icon(Icons.add),
      )
    );
  }
}

class JKShowData01 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    int counter = Provider.of<JKCounterViewModel>(context).counter;

    print('---Data01 的 Build 的方法---');

    return Container(
      color: Colors.brown,
      child: Text('当前计数：$counter', style: TextStyle(fontSize: 30, color: Colors.white),),
    );
  }
}

class JKShowData02 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    print('---Data02 的 Build 的方法---');

    return Container(
      color: Colors.green,
      child: Consumer<JKCounterViewModel>(
        builder: (ctx, counter, child){
          print("data02 Consumer build方法被执行");
          return Text('当前计数：${counter.counter}', style: TextStyle(fontSize: 30, color: Colors.white),);
        },
      ),
    );
  }
}


class JKShowData03 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.green,
      child: Consumer2<JKCounterViewModel, JKUserViewModel>(
        builder: (ctx, counter, user, child){
          return Text('当前计数：${counter.counter} 名字：${user.userModel.name}', style: TextStyle(fontSize: 30, color: Colors.white),);
        },
      ),
    );
  }
}

